package com.example.shushiyunsst.widget.verification.new_view

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.SeekBar
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.transition.Transition
import com.example.library_common.glidmodule.GlideApp
import com.example.shushiyunsst.R
import com.example.shushiyunsst.widget.verification.OnNewCaptchaMatchCallback
import com.example.shushiyunsst.widget.verification.view.IBaseView
import com.example.shushiyunsst.widget.verification.view.VerificationListener

class NewDragVerificationFrameView
@JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    LinearLayout(context, attrs, defStyleAttr), SeekBar.OnSeekBarChangeListener,
    INewVerificationView, OnNewCaptchaMatchCallback {

    private val mSwipeCaptchaView: NewSwipeCaptchaView

    private val mSeekBar: SeekBar

    private var mListener: NewVerificationListener? = null

    init {
        inflate(context, R.layout.new_drag_verification_frame_view, this)
        mSwipeCaptchaView = findViewById(R.id.mSwipeCaptchaView)
        mSeekBar = findViewById(R.id.mSeekBar)
        mSeekBar.setOnSeekBarChangeListener(this)
        mSwipeCaptchaView.setOnCaptchaMatchCallback(this)
    }

    override fun onProgressChanged(p0: SeekBar?, progress: Int, p2: Boolean) {
        mSwipeCaptchaView.setCurrentSwipeValue(progress)
//        mSwipeCaptchaView.setSmallImageLeft(p1)
    }

    override fun onStartTrackingTouch(p0: SeekBar?) {

        mSeekBar.max = mSwipeCaptchaView.getMaxSwipeValue()
    }

    override fun onStopTrackingTouch(p0: SeekBar?) {
        mSwipeCaptchaView.matchCaptcha()
//        val smallImageXWidth = mSwipeCaptchaView.getSmallImageXWidth()
//        mListener?.notifyResult(smallImageXWidth.toInt())
    }

    override fun setData(
        mBean: VerificationViewBean,
        listener: NewVerificationListener?
    ) {
        mListener = listener

        GlideApp.with(context)
            .asBitmap()
            .load(mBean.imgUrl)
            .into(object :CustomViewTarget<NewSwipeCaptchaView,Bitmap>(mSwipeCaptchaView){
                override fun onLoadFailed(errorDrawable: Drawable?) {

                }

                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    mSwipeCaptchaView.setImageBitmap(resource)
                    mSwipeCaptchaView.createCaptcha()
                }

                override fun onResourceCleared(placeholder: Drawable?) {

                }

            })
    }

    override fun dismiss() {

    }

    override fun generateView(): View {
        return this
    }

    override fun matchSuccess(swipeCaptchaView: NewSwipeCaptchaView?) {
        swipeCaptchaView?.resetCaptcha()
        mSeekBar.progress = 0
        mListener?.notifyStatusResult(VerificationListener.SUCCESS_STATUS)
    }

    override fun matchFailed(swipeCaptchaView: NewSwipeCaptchaView?) {
        swipeCaptchaView?.resetCaptcha()
        mSeekBar.progress = 0
        mListener?.notifyStatusResult(VerificationListener.FAIL_STATUS)
    }
}